﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace eKanbanWPF.View
{
    /// <summary>
    /// Interaction logic for BasePhaseGUI.xaml
    /// </summary>
    public partial class BasePhaseGUI : UserControl
    {
        private eKanbanWPF.Model.BasePhase phase = null;

        void phase_Changed(eKanbanWPF.Model.BasePhase sender)
        {
            if (sender != null)
            {
                ToolTip = sender.Name;
                Width = sender.Width;
                Height = sender.Height;
                Canvas.SetLeft(this, sender.Left);
                Canvas.SetTop(this, sender.Top);
                txtPhaseName.Text = sender.Name;
            }
            else
            {
                ToolTip = "";
            }
        }

        private void setPhase(eKanbanWPF.Model.BasePhase value)
        {
            if (phase != null)
            {
                phase.Changed -= phase_Changed;
            }
            phase = value;
            if (phase != null)
            {
                phase.Changed += phase_Changed;
            }
            phase_Changed(value);
        }


        public eKanbanWPF.Model.BasePhase Phase
        {
            get { return phase; }
            set
            {
                setPhase(value);
            }
        }

        public BasePhaseGUI()
        {
            InitializeComponent();

            setEvents();
        }

        private void setEvents()
        {
            this.MouseDoubleClick += UserControl_MouseDoubleClick;
            this.MouseUp += UserControl_MouseUp;
            this.PreviewMouseDown += UserControl_PreviewMouseDown;
            this.LostFocus += UserControl_LostFocus;
        }

        private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var r = FrmPhase.Show(phase);
            if (r == true)
            {
                phase.Name = FrmPhase.frmPhase.PhaseName;
            }
        }

        private void UserControl_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            Selector.SetIsSelected((Control)sender, true);
            ((Control)sender).Focus();
        }

        private void UserControl_LostFocus(object sender, RoutedEventArgs e)
        {
            Selector.SetIsSelected((Control)sender, false);
            if (phase != null)
            {
                phase.Width = this.Width;
                phase.Height = this.Height;
            }

        }

        private void UserControl_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //MessageBox.Show(Canvas.GetLeft(this).ToString());
        }
    }
}
